import re

class PatConvertor(object):
    def batchConvert(self,filename):
        '''
        convert all the log samples
        :param filename:
        :return:
        '''
        pass


    def verify(self,buf,regstr):
        m = re.search(regstr,buf)
        print(m.group(0))


    def convert(self,buf):
        '''
        convert log sample buf to a rule string
        :param buf: log sample
        :return: rule str
        '''
        m_before_equal = re.findall('\S+(?==)', buf)  # match string before  '='
        m_after_equal = re.findall('(?<==)\S*', buf)  # match string after '='

        print(m_before_equal)
        print(m_after_equal)
        if len(m_before_equal) != len(m_after_equal):
            print(len(m_before_equal))
            print(len(m_after_equal))

        reslist = []
        for tag in m_before_equal:
            if tag != 'GenTime':
                reslist.append(tag + '=(' + '[\S\s]*' + ')' + ' ')
            else:#"2016-09-28 05:05"
                reslist.append(tag + '="(' + '[\S\s]*' + ')"' + ' ')
        resStr = r''.join(reslist)
        if resStr[len(resStr) - 1] == ' ':
            resStr = resStr[0:len(resStr) - 1]

        return resStr

